package com.fmi.timedealer.track;

import java.util.Calendar;
import java.util.Date;

import com.fmi.timedealer.ActivityItem;
import com.fmi.timedealer.R;
import com.fmi.timedealer.achievement.AchievementsActivity;
import com.fmi.timedealer.datamanager.IDataManager;
import com.fmi.timedealer.datamanager.SQLiteDataManager;
import com.fmi.timedealer.notifications.PlansNotificationManager;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;

public class TrackPastEventActivity extends Activity {
	
	private IDataManager dbManager = null;
	private ActivityItem item = null;
	
	private void checkAchievements() {
		AchievementsActivity.checkNotCompleted(this);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.track_past_event);
		
		dbManager = new SQLiteDataManager("MyDB", this);
		
		Bundle extras = getIntent().getExtras();
		item = (ActivityItem)extras.getSerializable(ActivityItem.ACTIVITY_ITEM);
		
		if (item != null) {
			this.setTitle("Tracking time for " + item.getName());
		}
		
		Button addActionButton = (Button)findViewById(R.id.btnTrackPastEvent);
		addActionButton.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				
				Date startDate = getDateByPicker(R.id.datePickerTrack, R.id.timePickerTrack);
				Date endDate = calculateEndDate(startDate);
				if (endDate != null) {
					boolean success = dbManager.trackTime(item, startDate,
							endDate);
					checkAchievements();
					PlansNotificationManager.getInstance().setNotification(
							getApplicationContext());
					
					if (success) {
						Toast.makeText(
								getApplicationContext(),
								"Successfully tracked time for action "
										+ item.getName(), Toast.LENGTH_SHORT)
								.show();
					} else {
						Toast.makeText(
								getApplicationContext(),
								"Failed to track event time for action "
										+ item.getName(), Toast.LENGTH_SHORT)
								.show();
					}
				} else {
					Toast.makeText(getApplicationContext(), "Incorrect period input", Toast.LENGTH_SHORT).show();
				}
			}
		});
	}
	
	private Date getDateByPicker(int datePickerId, int timePickerId) {
		DatePicker datePicker = (DatePicker)findViewById(datePickerId);
		TimePicker timePicker = (TimePicker)findViewById(timePickerId);
		return new Date(datePicker.getYear() - 1900, datePicker.getMonth(), datePicker.getDayOfMonth(),
				timePicker.getCurrentHour(), timePicker.getCurrentMinute());
	}
	
	private Date calculateEndDate(Date startDate) {
		int weeks = 0, days = 0, hours = 0;
		EditText etPeriod = (EditText)findViewById(R.id.periodText);
		String period = etPeriod.getText().toString();
		String[] tokens = period.split(" ");
		try {
			for (String token : tokens) {
				if (token.endsWith("d")) {
					days = Integer.parseInt(token.substring(0, token.length() - 1));
				} else if (token.endsWith("w")) {
					weeks = Integer.parseInt(token.substring(0, token.length() - 1));
				} else if (token.endsWith("h")) {
					hours = Integer.parseInt(token.substring(0, token.length() - 1));
				} else if (!token.equals("")) {
					return null;
				}
			}
		} catch (Exception e) {
			Log.e(getClass().getSimpleName(), "Couldn't parse the period string " + period);
			return null;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(startDate);
		cal.add(Calendar.HOUR, hours);
		cal.add(Calendar.DATE, days + 7 * weeks);
		return cal.getTime();
	}
}
